package com.tueye.web.controller.blog;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.tueye.entity.user.User;
import com.tueye.entity.user.WebSiteEmailInfo;
import com.tueye.service.userblog.IWebSiteEmailService;
import com.tueye.utils.UUIDGenerator;
import com.tueye.utils.servlet.CaptchaServiceSingleton;

/**
 * 
 * 站内信
 * 
 * @author hanxiaowei
 * 
 */
@SuppressWarnings("unchecked")
@org.springframework.stereotype.Controller
public class WebSiteEmailController {

	private Log log = LogFactory.getLog(WebSiteEmailController.class);

	@Autowired
	@Qualifier("webSiteEmailService")
	IWebSiteEmailService webSiteEmailService;

	@RequestMapping("/turnInbox.do")
	public ModelAndView inbox(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView("/pages/topics/userblog/inbox");
		List list = this.webSiteEmailService.getInboxInfo("F91AAD08E928FC317C12366082489061");
		request.setAttribute("boxdata", list);
		return mav;
	}

	@RequestMapping("/turnSend.do")
	public ModelAndView send(HttpServletRequest request,
			Model model) throws Exception {
			ModelAndView mav = new ModelAndView("/pages/topics/userblog/send");
			String blogusername = request.getParameter("blogusername");//接收者名称
			model.addAttribute("blogusername", blogusername);
		return mav;
	}
	
	@RequestMapping("/saveSend.do")
	public String saveSendInfo(HttpServletRequest request,
			HttpServletResponse response ){
		
		String receiveusername = request.getParameter("receiveusername");
		String sendusername = request.getParameter("sendusername");
		String sendtitle = request.getParameter("sendtitle");
		String sendcontent = request.getParameter("sendcontent");
		String validateCode = request.getParameter("validateCode");
		try{
			boolean flag = CaptchaServiceSingleton.getInstance().validateCaptchaResponse(validateCode, request.getSession());
			if(!flag){
				response.getWriter().write("1");   
			}else{
				WebSiteEmailInfo webSiteEmailInfo = new WebSiteEmailInfo();
				User user = new User();
				if(receiveusername.indexOf(",")==-1){
					user = webSiteEmailService.getUserToUsername(receiveusername);
					webSiteEmailInfo.setUserid(user.getId());
					webSiteEmailInfo.setContent(sendcontent);
					webSiteEmailInfo.setTitle(sendtitle);
					webSiteEmailInfo.setId(new UUIDGenerator().getUUID());
					webSiteEmailInfo.setIfread(1);
					webSiteEmailInfo.setSys_time(new Date());
					webSiteEmailInfo.setSendUserName(sendusername);
					webSiteEmailService.saveWebSiteEmail(webSiteEmailInfo);
				}else{
					String[] receiveusernameArry = receiveusername.split(",");
					for (int i = 0; i < receiveusernameArry.length; i++) {
						if(StringUtils.isNotEmpty(receiveusernameArry[i])){
							user = webSiteEmailService.getUserToUsername(receiveusernameArry[i]);
							webSiteEmailInfo.setUserid(user.getId());
							webSiteEmailInfo.setContent(sendcontent);
							webSiteEmailInfo.setTitle(sendtitle);
							webSiteEmailInfo.setId(new UUIDGenerator().getUUID());
							webSiteEmailInfo.setIfread(1);
							webSiteEmailInfo.setSys_time(new Date());
							webSiteEmailInfo.setSendUserName(sendusername);
							webSiteEmailService.saveWebSiteEmail(webSiteEmailInfo);
						}
					}
				}
			}
		}catch (Exception e) { 
			log.error("发送站内信出错！======"+e.getMessage());
			
		}
		return null;
	}
}
